package com.zfzs.post.modules.address.service.impl;

import com.zfzs.post.modules.address.domain.SysCity;
import com.zfzs.post.modules.address.repository.SysCityRepository;
import com.zfzs.post.modules.address.service.SysCityService;
import com.zfzs.post.modules.address.service.dto.SysCityDTO;
import com.zfzs.post.modules.address.service.dto.SysCityQueryCriteria;
import com.zfzs.post.modules.address.service.mapper.SysCityMapper;
import com.zfzs.post.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;

/**
* @author ck
* @date 2019-09-09
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SysCityServiceImpl implements SysCityService {

    @Autowired
    private SysCityRepository sysCityRepository;

    @Autowired
    private SysCityMapper sysCityMapper;

    @Override
    public Object queryAll(SysCityQueryCriteria criteria, Pageable pageable){
        Page<SysCity> page = sysCityRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(sysCityMapper::toDto));
    }

    @Override
    public Object queryAll(SysCityQueryCriteria criteria){
        return sysCityMapper.toDto(sysCityRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public SysCityDTO findById(Long id) {
        Optional<SysCity> sysCity = sysCityRepository.findById(id);
        ValidationUtil.isNull(sysCity,"SysCity","id",id);
        return sysCityMapper.toDto(sysCity.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysCityDTO create(SysCity resources) {
        return sysCityMapper.toDto(sysCityRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysCity resources) {
        Optional<SysCity> optionalSysCity = sysCityRepository.findById(resources.getCityId());
        ValidationUtil.isNull( optionalSysCity,"SysCity","id",resources.getCityId());
        SysCity sysCity = optionalSysCity.get();
        sysCity.copy(resources);
        sysCityRepository.save(sysCity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        sysCityRepository.deleteById(id);
    }

    @Override
    public List<SysCity> queryProvinceList() {
        return sysCityRepository.queryProvinceList();
    }

    @Override
    public List<SysCity> queryCityOrArea(Long id) {
        return sysCityRepository.queryCityOrArea(id);
    }
}